package com.op.parcel.manage.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.op.parcel.common.utils.AssertUtil;
import com.op.parcel.manage.modules.sys.entity.Permission;
import com.op.parcel.manage.modules.sys.entity.User;
import com.op.parcel.manage.modules.sys.entity.UserRole;
import com.op.parcel.manage.modules.sys.mapper.PermissionMapper;
import com.op.parcel.manage.modules.sys.mapper.UserMapper;
import com.op.parcel.manage.modules.sys.service.IRoleService;
import com.op.parcel.manage.modules.sys.service.IUserRoleService;
import com.op.parcel.manage.modules.sys.service.IUserService;
import com.op.parcel.manage.modules.sys.vo.UserVO;
import com.op.parcel.manage.modules.sys.wrapper.UserWrapper;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务实现类
 *
 * @author xuan
 * @since 2019-01-07
 */
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

	IRoleService roleService;

	PermissionMapper permissionMapper;

	IUserRoleService userRoleService;

	PasswordEncoder passwordEncoder;


	@Override
	public UserVO getUserInfo(User user) {
		User u = baseMapper.selectOne(new QueryWrapper<>(user));
		if (u == null) {
			throw new UsernameNotFoundException("用户不存在");
		}
		UserWrapper userWrapper = new UserWrapper(roleService);
		UserVO userVO = userWrapper.entityVO(u);
		userVO.setPassword(u.getPassword());
		if (u.getId() != null) {
			List<Permission> permissionList = permissionMapper.findByUserId(userVO.getId());
			userVO.setPermissions(permissionList);
		}
		return userVO;
	}

	@Override
	public void updatePwd(User user) {
		User u = baseMapper.selectById(user.getId());
		u.setPassword(passwordEncoder.encode(user.getPassword()));
		AssertUtil.notZero(baseMapper.updateById(u), "修改失败");
	}

	@Transactional
	@Override
	public void saveOrUpdateUser(User user, List<String> roleIds) {

		if (StringUtils.isNotEmpty(user.getPassword())) {
			user.setPassword(passwordEncoder.encode(user.getPassword()));
		}
		if (StringUtils.isNotEmpty(user.getId())) {
			UserRole userRole = new UserRole();
			userRole.setUserId(user.getId());
			QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>(userRole);
			userRoleService.remove(userRoleQueryWrapper);
		}
		this.saveOrUpdate(user);
		List<UserRole> userRoles = new ArrayList<>();
		roleIds.forEach(roleId -> {
			UserRole userRole = new UserRole();
			userRole.setUserId(user.getId());
			userRole.setRoleId(roleId);
			userRoles.add(userRole);
		});
		userRoleService.saveOrUpdateBatch(userRoles);
	}
}
